﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using TalentDesk.Contracts.Ef;
using TalentDesk.Contracts.ServiceContracts;
using TalentDesk.WcfService.EFQueries;
using TalentDesk.Data;

namespace TalentDesk.WcfService.Implementation
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    internal class ProjectService : ProjectBaseClass, IProjectService
    {




        public ProjectType AddProjectType(ProjectType item)
        {
            try
            {
                BeginTransaction();

                TalentDeskContext.ProjectTypes.ApplyChanges(item);
                TalentDeskContext.SaveChanges();
                CommitTransaction();
                return item;
            }
            catch (Exception ex)
            {
                RollbackTransaction();
                throw ex;
            }
        }

        public List<Project> GetProjects(bool p_bInclude = false)
        {
            if (p_bInclude)
            {
                return ProjectServiceEFQueries.ProjectsByIsInclude_CQ(TalentDeskContext).AsEnumerable().ToList();
            }
            return ProjectServiceEFQueries.Projects_CQ(TalentDeskContext).AsEnumerable().ToList();
        }

        public Project GetProjectByProjectId(int p_iProjectId, bool p_bInclude = false)
        {
            if (p_bInclude)
            {
                return ProjectServiceEFQueries.ProjectsByIsIncludeAndProjectId_CQ(TalentDeskContext, p_iProjectId).AsEnumerable().FirstOrDefault();
            }
            return ProjectServiceEFQueries.ProjectsByProjectId_CQ(TalentDeskContext, p_iProjectId).AsEnumerable().FirstOrDefault();
        }

        public int SaveProject(Project p_oProject, List<ProjectTeam> p_lProjectTeams = null, List<Attachment> p_lAttachments = null)
        {
            try
            {
                BeginTransaction();
               
                TalentDeskContext.Projects.ApplyChanges(p_oProject);

                foreach (var item in p_lProjectTeams)
                {
                    TalentDeskContext.ProjectTeams.ApplyChanges(item);
                }

                foreach (var item in p_lAttachments)
                {
                    TalentDeskContext.Attachments.ApplyChanges(item);
                }
                TalentDeskContext.SaveChanges();
                CommitTransaction();
                return 0;
            }
            catch (Exception ex)
            {
                RollbackTransaction();
                throw ex;
            }
        }


        public List<ProjectTeam> GetProjectTeamsByProjectId(int p_iProjectId, bool p_bInclude = false)
        {
            if (p_bInclude)
            {
                return TalentDeskContext.ProjectTeams.Include("Project").Where(p => p.ProjectId == p_iProjectId).ToList();
            }
            return TalentDeskContext.ProjectTeams.Where(p => p.ProjectId == p_iProjectId).ToList();
        }

        public void SaveProjectTeam(ProjectTeam p_oProjectTeam)
        {
            try
            {
                BeginTransaction();

                TalentDeskContext.ProjectTeams.ApplyChanges(p_oProjectTeam);


                TalentDeskContext.SaveChanges();
                CommitTransaction();

            }
            catch (Exception ex)
            {
                RollbackTransaction();
                throw ex;
            }
        }


    }
}
